import defaultSettings from '@/settings'
import Cookies from 'js-cookie'

const { showSettings, fixedHeader, sidebarLogo } = defaultSettings
const defaultTheme = '#3673f4'
const themeKey = 'theme-color'

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  theme: Cookies.get(themeKey) || defaultTheme
}

const mutations = {
  // 万能的mutation...
  // this.$store.commit('CHANGE_SETTING', {key: 'theme', value: xxx})
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }

    // 确定你改的是theme
    if (key === 'theme') {
      Cookies.set(themeKey, value)
    }
  },
  // 提供一个重置主题色的mutation
  resetTheme(state) {
    state.theme = defaultTheme
    Cookies.remove(themeKey)
  }
  // setTheme(state, val) {
  //   state.theme = val
  // }
}

const actions = {
  // this.$store.dispatch('changeSetting', {key: 'theme', value: xxx})
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

